﻿
using System;
using System.Collections.Generic;
using System.Linq;
using RBAC.DAL.Entities;
using NHibernate;
using NHibernate.Criterion;
using RBAC.DAL.Models;


namespace RBAC.DAL.Dal
{
    public class RoleUserDAL
    {
        private readonly ISession _session;
        public RoleUserDAL(ISession session)
        {
            _session = session;
        }

        public List<RoleUser> GetAll(RoleUserSearch searchCriteria)
        {
            var criteria = _session.CreateCriteria(typeof(RoleUser));

            if (searchCriteria != null)
            {
                #region Search
                //Pk
                //if (!string.IsNullOrEmpty(searchCriteria.RoleCode))
                //{
                //    if (searchCriteria.EqPk.GetValueOrDefault())
                //    {
                //        criteria.Add(Restrictions.Eq("RoleCode", searchCriteria.RoleCode));
                //    }
                //    else
                //    {
                //        criteria.Add(Restrictions.Like("RoleCode", searchCriteria.RoleCode, MatchMode.Anywhere));
                //    }
                //}
                //lookup to parent
                if (null != searchCriteria.Role)
                {
                    criteria.Add(Restrictions.Eq("Role", searchCriteria.Role));
                }
                if (null != searchCriteria.User)
                {
                    criteria.Add(Restrictions.Eq("User", searchCriteria.User));
                }
                if (searchCriteria.IsActive != null)
                {
                    criteria.Add(Restrictions.Eq("IsActive", searchCriteria.IsActive));
                }
                #endregion

                #region Sort
                if (searchCriteria.IsSort)
                    criteria.AddOrder(searchCriteria.Order);
                #endregion
            }
            return criteria.List<RoleUser>().ToList();
        }


        public void Add(RoleUser role)
        {
            if (role != null)
            {
                role.CreateDate = DateTime.Now;
                //if (role.CreateBy != null) role.CreateBy = _session.Get<User>(role.CreateBy.Id);
            }
            _session.Save(role);
        }

        public void Update(RoleUser role)
        {

            // chlidren

            var obj = _session.Get<Role>(role.Id);
            if (obj == null)
                throw new Exception("Update Not Success");
            //role.Permissions = new List<Permission>(obj.Permissions);

            role.CreateDate = obj.CreateDate;
            role.CreateById = obj.CreateById;
            role.ProjectId = obj.ProjectId;
            _session.Merge(role);

        }

        public void Delete(List<string> idList)
        {
            int row = _session.CreateQuery("DELETE RBAC.DAL.Entities.Role where Id in (:IdList)").SetParameterList("IdList", idList).ExecuteUpdate();
            if (row != idList.Count)
                throw new Exception("Delete Not Success");
        }
    }
}

